﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml;
using System.IO;

namespace MyXMLPrase
{
    class TestSchema
    {
        private static string Namespace = "MyXMLPrase";
        private static bool isValid = true;
        public static bool ValidationSchema(string filename)
        {
            XmlTextReader r = new XmlTextReader(filename);
            XmlValidatingReader v = new XmlValidatingReader(r);
            v.ValidationType = ValidationType.Schema;
            v.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);
            while (v.Read())
            {
                // Can add code here to process the content.
            }
            v.Close();
            return isValid;
        }
        public static bool ValidationSchemaNew(string filename, string schema)
        {
            XmlSchemaSet xsSet = new XmlSchemaSet();
            try
            {
                xsSet.Add(Namespace, schema);
            }
            catch (Exception ex)
            {
                string a = ex.Message;
                return false;
            }
            // 定义公文模式的使用方式
            XmlReaderSettings xrSetting = new XmlReaderSettings();
            xrSetting.ValidationType = ValidationType.Schema;
            // 关联验证读取器与架构集合
            xrSetting.Schemas = xsSet;
            // 添加发生错误时的事件处理程序
            xrSetting.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);
            // 使用最新的方式来构建可进行校验的读取器并构造验证读取器
            XmlReader xr = XmlReader.Create(filename, xrSetting);
            // 循环检测所有的文档节点
            while (xr.Read())
            {
            }
            xr.Close();
            return isValid;
        }
        public static bool ValidationSchemaNow(string xmlstring, string schemastring)
        {
            //构建待验证数据的XmlReader对象
            XmlReader xmlDataReader = XmlTextReader.Create(new StringReader(xmlstring));
            //构建标准的XmlReader对象
            XmlReader xmlSchemaReader = XmlTextReader.Create(new StringReader(schemastring));
            // 定义公文模式的使用方式
            XmlReaderSettings xrSetting = new XmlReaderSettings();
            try
            {
                xrSetting.Schemas.Add(null, xmlSchemaReader);
            }
            catch (System.Xml.Schema.XmlSchemaException ex)
            {
                string b = ex.Message;
                return false;
            }
            xrSetting.ValidationType = ValidationType.Schema;
            xrSetting.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);
            XmlReader xr = XmlReader.Create(xmlDataReader, xrSetting);
            while (xr.Read())
            {
            }
            xr.Close();
            return isValid;
        }
        /// <summary>
        /// 验证处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private static void MyValidationEventHandler(object sender, ValidationEventArgs args)
        {
            if (args.Severity == XmlSeverityType.Warning)
            {
                WriteErrorLogs("Validation Warning:   " + args.Message);
            }
            else
            {
                WriteErrorLogs("Validation Error:   " + args.Message);
            }
            isValid = false;
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="ss"></param>
        private static void WriteErrorLogs(string ss)
        {
            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter(@"error.txt", true, Encoding.UTF8);
                sw.WriteLine("[" + DateTime.Now.ToString() + "]" + ss);
                sw.Flush();
                sw.Close();
            }
            catch { sw.Close(); }
        }
    }
}
